﻿
//***********************************************************************
// * Module:   MsjWjWsbaHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/29
// * Purpose: Definition of the Class MsjWjWsbaHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// MsjWjWsbaHandler 的摘要说明
    /// </summary>
    public class MsjWjWsbaHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                var mOrg = AppOrganizatiOnBLL.Load(mUser.OrgnaId);
                MsjWjWsba mMsjWjWsba = new MsjWjWsba();
                mMsjWjWsba.Dwid = Guid.NewGuid().ToString(); //主键
                mMsjWjWsba.Dwbh = context.Request.Form["texDwbh"]; //单位编号
                mMsjWjWsba.Dwmc = context.Request.Form["texDwmc"]; //单位(个人)名称
                mMsjWjWsba.Dwdz = context.Request.Form["texDwdz"]; //单位地址
                mMsjWjWsba.Sjzgbm = context.Request.Form["texSjzgbm"]; //上级主管部门（选择）
                mMsjWjWsba.Jyxz = context.Request.Form["texJyxz"]; //经营性质(数据字典)
                mMsjWjWsba.Sfzh = context.Request.Form["texSfzh"]; //公民身份证号
                mMsjWjWsba.Frxm = context.Request.Form["texFrxm"]; //法人（负责人）
                mMsjWjWsba.Lxdh = context.Request.Form["texLxdh"]; //联系电话
                mMsjWjWsba.Zafzr = context.Request.Form["texZafzr"]; //治安负责人
                mMsjWjWsba.Yyzzzchm = context.Request.Form["texYyzzzchm"]; //经营范围-主营
                mMsjWjWsba.Gajgdjsj = context.Request.Form["texGajgdjsj"]; //经营范围-兼营
                mMsjWjWsba.Gajgjsr = context.Request.Form["texGajgjsr"]; //是否具备许可条件
                mMsjWjWsba.Qyjbr = context.Request.Form["texQyjbr"]; //材料是否齐全
                mMsjWjWsba.Cjsj = DateTime.Now.ToString();
                mMsjWjWsba.Cjr = mUser.AppuserId;
                mMsjWjWsba.Cjrdw = mOrg.OrgnaName; //回复人单位
                mMsjWjWsba.Enabled = Enabled.TRUE; //是否有效（1有效，0无效）
                mMsjWjWsba.Movesign = "0"; //是否传输（1已，0未）

                mResultModel = MsjWjWsbaBLL.Insert(mMsjWjWsba, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                MsjWjWsba mMsjWjWsba = new MsjWjWsba();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = MsjWjWsbaBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion
        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            MsjWjWsba mMsjWjWsba = new MsjWjWsba();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mMsjWjWsba = MsjWjWsbaBLL.Load(paramKey); //根据键值获取实体信息
                if (mMsjWjWsba != null)
                {
                    var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                    var mOrg = AppOrganizatiOnBLL.Load(mUser.OrgnaId);
                    mMsjWjWsba.Dwbh = context.Request.Form["texDwbh"]; //单位编号
                    mMsjWjWsba.Dwmc = context.Request.Form["texDwmc"]; //单位(个人)名称
                    mMsjWjWsba.Dwdz = context.Request.Form["texDwdz"]; //单位地址
                    mMsjWjWsba.Sjzgbm = context.Request.Form["texSjzgbm"]; //上级主管部门（选择）
                    mMsjWjWsba.Jyxz = context.Request.Form["texJyxz"]; //经营性质(数据字典)
                    mMsjWjWsba.Sfzh = context.Request.Form["texSfzh"]; //公民身份证号
                    mMsjWjWsba.Frxm = context.Request.Form["texFrxm"]; //法人（负责人）
                    mMsjWjWsba.Lxdh = context.Request.Form["texLxdh"]; //联系电话
                    mMsjWjWsba.Zafzr = context.Request.Form["texZafzr"]; //治安负责人
                    mMsjWjWsba.Yyzzzchm = context.Request.Form["texYyzzzchm"]; //经营范围-主营
                    mMsjWjWsba.Gajgdjsj = context.Request.Form["texGajgdjsj"]; //经营范围-兼营
                    mMsjWjWsba.Gajgjsr = context.Request.Form["texGajgjsr"]; //是否具备许可条件
                    mMsjWjWsba.Qyjbr = context.Request.Form["texQyjbr"]; //材料是否齐全
                    mMsjWjWsba.Xgr = mUser.AppuserId;
                    mMsjWjWsba.Xgrdw = mOrg.OrgnaName; //回复人单位
                    mMsjWjWsba.Xgsj = DateTime.Now.ToString();
                    mMsjWjWsba.Enabled = Enabled.TRUE; //是否有效（1有效，0无效）
                    mMsjWjWsba.Movesign = "0"; //是否传输（1已，0未）

                    mResultModel = MsjWjWsbaBLL.Update(mMsjWjWsba, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                MsjWjWsba mMsjWjWsba = new MsjWjWsba(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mMsjWjWsba = MsjWjWsbaBLL.Load(paramKey); //根据键值获取管理员
                    if (mMsjWjWsba != null)
                    {
                        json = JsonConvert.SerializeObject(mMsjWjWsba);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string DWMC = context.Request.Form["texTitle"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtMsjWjWsba = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            SqlConditionList sqllist = new SqlConditionList();
            if (!string.IsNullOrEmpty(DWMC)) ////审核状态
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjWjWsbaConst.Columns.Dwmc, DWMC), SqlLogicOper.AND_LOGIC);
            }
            dtMsjWjWsba = Q.GetRecordByPage("*", sqllist.ToSql(), "", intPageSize, intPageIndex, MsjWjWsbaConst.TableName, MsjWjWsbaConst.FieldKey, "").Tables[0];
            if (dtMsjWjWsba != null && dtMsjWjWsba.Rows.Count > 0)
            {
                for (int i = 0; i < dtMsjWjWsba.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtMsjWjWsba.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtMsjWjWsba;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }
        #endregion

    }
}


